#define _CRT_SECURE_NO_WARNINGS 1 
#include"Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNote* cur = pq->phead;
	while (cur)
	{
		QNote* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->size = 0;
	pq->phead = pq->ptail = NULL;
}
void QueuePush(Queue* pq, QDatatype x)
{
	assert(pq);
	QNote* newnote = (QNote*)malloc(sizeof(QNote));
	if (newnote == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnote->next = NULL;
	newnote->data = x;
	if (pq->ptail == NULL)
	{
		assert(pq->phead==NULL);
		pq->phead = pq->ptail = newnote;
	}
	else
	{
  		pq->ptail->next=newnote;
		pq->ptail = newnote;
	}
	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		QNote* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;

	}
	pq->size--;
}
QDatatype QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->phead->data;
}
QDatatype QueueBack(Queue* pq) 
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->ptail->data;
}
int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->size==0;
}